package com.stenio.fancat.storage;

import com.aliyun.oss.OSSClient;
import com.stenio.fancat.util.StringUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

@Component
public class Storage {

    /**
     * 转存指定微信公众号文章
     *
     * @param url 公众号地址
     * @return 转存后地址
     */
    public StoragePage storage(String url) {
        Connection connect = Jsoup.connect(url);
        Document doc = null;
        String firstImageUrl = null;
        try {
            doc = connect.get();
            Elements pngs = doc.select("img[data-src]");
            for (Element png : pngs) {
                String imageUrl = png.attr("data-src");
                if (!StringUtil.isEmpty(imageUrl)) {
                    String localImageUrl = saveImage(imageUrl);
                    if (firstImageUrl == null) {
                        firstImageUrl = localImageUrl;
                    }
                    png.attr("data-src", localImageUrl);
                }
            }
            String htmlUrl =  saveHtml(doc.html());
            StoragePage page = new StoragePage();
            page.setHtmlUrl(htmlUrl);
            page.setFirstImageUrl(firstImageUrl);
            return page;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private String saveHtml(String htmlContent) {
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = "LTAIFA7GyAsSqnRT";
        String accessKeySecret = "wBJGb16aDePrvfzRR0Z1gjGVHf6F1t";
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传字符串。
        String htmlName = StringUtil.getUUID() + ".html";
        ossClient.putObject("fancat", htmlName, new ByteArrayInputStream(htmlContent.getBytes()));
        // 关闭OSSClient。
        ossClient.shutdown();
        return "https://fancat.oss-cn-beijing.aliyuncs.com/" + htmlName;
    }

    private String saveImage(String url) {
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = "LTAIFA7GyAsSqnRT";
        String accessKeySecret = "wBJGb16aDePrvfzRR0Z1gjGVHf6F1t";
        // 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传网络流。
        try {
            URL uri = new URL(url);
            InputStream inputStream = new URL(url.toString()).openStream();
            String uuid = StringUtil.getUUID() + ".gif";
            System.out.println(uuid);
            ossClient.putObject("fancat", uuid, inputStream);
            return "https://fancat.oss-cn-beijing.aliyuncs.com/" + uuid;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient。
            ossClient.shutdown();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(new Storage().storage("https://mp.weixin.qq.com/s/Nk6MsTIw5qouyTa8EBPojg"));
    }

}
